#include "showque.h"
#include "ui_showque.h"

ShowQue::ShowQue(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShowQue)
{
    ui->setupUi(this);
    this->setWindowTitle("查看试题库");
    db = Database::getInstance().getDatabase();


}

ShowQue::~ShowQue()
{
    delete ui;
}

void ShowQue::on_pushButton_clicked()
{
    QString sql = "select * from questions";
    QSqlQuery query(db);
    if(!query.exec(sql))
        QMessageBox::information(this,"查看","Error"+db.lastError().text());
    int line_count = ui->tableWidget->rowCount();
    //清空表格
    for(int i=0;i<line_count;i++)
        ui->tableWidget->removeRow(0);
    while(query.next())
    {
        qDebug() << query.value(0).toString() << query.value(1).toString();
        ui->tableWidget->insertRow(0);//插入0行
        QTableWidgetItem *item0 = new QTableWidgetItem(query.value(0).toString());
        QTableWidgetItem *item1 = new QTableWidgetItem(query.value(1).toString());
        QTableWidgetItem *item2 = new QTableWidgetItem(query.value(2).toString());
        ui->tableWidget->setItem(0,0,item0);
        ui->tableWidget->setItem(0,1,item1);//设置item
        ui->tableWidget->setItem(0,2,item2);
    }
}

void ShowQue::on_pushButton_2_clicked()
{
    emit show_back();
}
